package texts;

import moment.Utiles;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * 文章详情页面测试
 */
public class DetailsPage extends Utiles {
    private static String url = "http://110.41.84.205:9091/index.html";
    public DetailsPage() {
        super(url);
    }
    // 打开
    public void detailsPageRight() {
        String text = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        assert "首页".equals(text);
    }

    public void detailsPageSec() throws InterruptedException {
        // 进入详情页面
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        // 点赞
        driver.findElement(By.cssSelector("#details_btn_like_count")).click();
        Thread.sleep(100);
        // 校验点赞
        assert driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText().equals("成功");
    }

    /**
     * 帖子回复功能
     * @throws InterruptedException
     */
    public void recoverPageSec() throws InterruptedException {
        // 刷新页面
        driver.navigate().refresh();
        // 进入详情页面
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        // 滚动页面
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向下拉到底
        js.executeScript("window.scrollTo(0,100000)");
        Thread.sleep(500);
        // 将WebDriver转换为JavaScriptExecutor

        // 定位输入框
        WebElement inputElement = driver.findElement(By.cssSelector("#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code"));
        // 模拟键盘点击和输入
        Actions action = new Actions(driver);
        action.sendKeys(inputElement, "发布测试").build().perform();

        Thread.sleep(300);
        // 点击回复
        driver.findElement(By.cssSelector("#details_btn_article_reply > svg")).click();
        // 校验
        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        assert text.equals("成功");
    }

    /**
     * 编辑
     */
    public void editingPageSec() throws InterruptedException {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
        // 刷新页面
        driver.navigate().refresh();
        // 进入详情页面
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        driver.findElement(By.cssSelector("#details_artile_edit")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2"),"编辑"));
        // 校验页面
        String text = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div > h2")).getText();

        // 修改测试
        WebElement element = driver.findElement(By.cssSelector("#edit_article_title"));
        element.clear();
        element.sendKeys("修改测试");

        // 滚动页面
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向下拉到底
        js.executeScript("window.scrollTo(0,100000)");
        Thread.sleep(500);
        driver.findElement(By.cssSelector("#edit_article_submit")).click();

        assert driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText().equals("成功");
    }

    /**
     * 帖子删除
     */
    public void deletePageSec() {
        // 刷新页面
        driver.navigate().refresh();
        // 进入详情页
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a")).click();
        // 校验
        assert driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(2) > h3")).getText().equals("最新回复");
        // 点击删除
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a")).click();
        // 确认删除
        driver.findElement(By.cssSelector("#details_artile_delete")).click();
        // 校验成功
        assert driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText().equals("成功");
    }
}
